import gtk

from Bases import BaseObject
from ...bases import simple

class Color(simple.Color):
    pass

class EntryBuffer(simple.EntryBuffer):
    pass
    
class TextBuffer(BaseObject):
    def __init__(self, **kwargs):
        super(TextBuffer, self).__init__(**kwargs)
        self.register_signal('modified')
        self.src_object = kwargs.get('src_object')
        self.src_attr = kwargs.get('src_attr')
        self.allow_obj_setattr = kwargs.get('allow_obj_setattr', False)
        self.id = kwargs.get('id')
        self.buffer = gtk.TextBuffer()
        self.buffer.connect('begin-user-action', self.on_begin_action)
        self.buffer.connect('end-user-action', self.on_end_action)
        self.widget = kwargs.get('widget')
        if self.widget is not None:
            self.widget.set_buffer(self.buffer)
        
        if self.src_object is not None and self.src_attr is not None:
            self.update_text_from_object()
    
    def get_text(self):
        iters = [self.buffer.get_start_iter(), self.buffer.get_end_iter()]
        self._modified = False
        return self.buffer.get_text(*iters)
    
    def set_text(self, text):
        self.buffer.set_text(str(text))
        self._modified = False
    
    def update_text_from_object(self, *args, **kwargs):
        obj_text = getattr(self.src_object, self.src_attr)
        if obj_text != self.get_text():
            self.set_text(obj_text)
    
    def update_object_from_text(self, *args, **kwargs):
        obj_text = getattr(self.src_object, self.src_attr)
        bfr_text = self.get_text()
        if bfr_text != obj_text:
            setattr(self.src_object, self.src_attr, bfr_text)
        
    def on_begin_action(self, *args):
        #print 'action begin'
        self._modified = True
    
    def on_end_action(self, *args):
        text = self.get_text()
        #print 'action end'
        #self._modified = True
        if self.allow_obj_setattr:
            #setattr(self.src_object, self.src_attr, text)
            self.update_object_from_text()
        self.emit('modified', id=self.id, text=text)
        
#    def update_name_from_buffer(self):
#        buffer = self.textBufferName
#        if self.currentParameter is not None:# and buffer.get_modified is True:
#            text = self.get_buffer_text()
#            if text != self.currentParameter.name:
#                self.currentParameter.name = text
#                buffer.set_modified(False)
        

class Spin(simple.Spin):
    pass

class Radio(simple.Radio):
    def setup_widgets(self):
        group = None
        for key, val in self.attribute.parameters.iteritems():
            widget = gtk.RadioButton(group=group, label=val)
            id = widget.connect('clicked', self.on_widgets_clicked)
            self.widget_signals.update({val:id})
            if group is None:
                group = widget
            self.widgets.update({val:widget})

class Toggle(simple.Toggle):
    pass
    
fader_types = {'VSlider':gtk.VScale, 'HSlider':gtk.HScale}

class Fader(simple.Fader):
    def setup_widgets(self, **kwargs):
        fader_type = kwargs.get('fader_type')
        if not hasattr(self, 'widget'):
            self.widget = fader_types[fader_type]()
        #self.widget.set_draw_value(False)
        self.widget_packing = {'expand':True}
        
        adj_kwargs = {'value':0, 'lower':self.value_range[0], 'upper':self.value_range[1], 'step_incr':1}
        adj_kwargs.update(kwargs.get('adj_kwargs', {}))
        self.adj = gtk.Adjustment(**adj_kwargs)
        self.widget.set_adjustment(self.adj)
        
        id = self.widget.connect('change-value', self.on_widget_change_value)
        self.widget_signals.append(id)
        id = self.widget.connect('button-press-event', self.on_widget_button_press)
        self.widget_signals.append(id)
        id = self.widget.connect('button-release-event', self.on_widget_button_release)
        self.widget_signals.append(id)
        id = self.widget.connect('format-value', self.on_widget_format_value)
        self.widget_signals.append(id)
    
    def set_widget_value(self, value):
        if value is None:
            value = 0
        self.adj.set_value(value)
        
    def get_widget_value(self):
        return self.adj.get_value()
        
    def set_widget_range(self):
        self.adj.lower = self.value_range[0]
        self.adj.upper = self.value_range[1]
#        if self.attribute and type(self.attribute.value) == float:
#            print 'adj_step = .01'
#            self.adj.step_increment = .01
#        else:
#            self.adj.step_increment = 1
        
    def on_widget_format_value(self, widget, value):
        return '%.2f%s' % (value, self.value_symbol)
        
class ScaledFader(simple.ScaledFader):
    def setup_widgets(self, **kwargs):
        fader_type = kwargs.get('fader_type')
        if not hasattr(self, 'widget'):
            self.widget = fader_types[fader_type]()
        self.widget_packing = {'expand':True}
        
        adj_kwargs = {'value':0, 'lower':self.ui_scale['min'],
                      'upper':self.ui_scale['max'], 'step_incr':1}
        adj_kwargs.update(kwargs.get('adj_kwargs', {}))
        self.adj = gtk.Adjustment(**adj_kwargs)
        self.widget.set_adjustment(self.adj)
        
        id = self.widget.connect('change-value', self.on_widget_change_value)
        self.widget_signals.append(id)
        id = self.widget.connect('button-press-event', self.on_widget_button_press)
        self.widget_signals.append(id)
        id = self.widget.connect('button-release-event', self.on_widget_button_release)
        self.widget_signals.append(id)
    
    def on_widget_change_value(self, range, scroll, value):
        self.scaler.set_value('ui', value)
        #print 'widget: ', value
        return False
        
    def set_widget_value(self, value):
        self.adj.set_value(value)
    

class Meter(BaseObject):
    def __init__(self, **kwargs):
        super(Meter, self).__init__(**kwargs)
        
